import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Solution590 {

    static class Node {
        public int val;
        public List<Node> children;

        public Node() {}

        public Node(int _val) {
            val = _val;
        }

        public Node(int _val, List<Node> _children) {
            val = _val;
            children = _children;
        }
    }

    public List<Integer> postorder(Node root) {
        List<Integer> ans=new ArrayList<>();
        dfs(root,ans);
        return ans;
    }

    private void dfs(Node root, List<Integer> ans) {
        if (root==null){
            return;
        }
        if (root.children!=null) {
            for (Node child : root.children) {
                dfs(child, ans);
            }
        }
        ans.add(root.val);
    }

    public static void main(String[] args) {
        Node node5=new Node(5);
        Node node6=new Node(6);
        Node node3=new Node(3, Arrays.asList(node5,node6));
        Node node2=new Node(2);
        Node node4=new Node(4);
        Node node1=new Node(1,Arrays.asList(node3,node2,node4));
        System.out.println(new Solution590().postorder(node1));
    }
}
